//
//  XFDNodeMenu.m
//  XFDHomeWorkPlatfrom_Example
//
//  Created by 朱国良 on 2017/11/2.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDNodeMenu.h"
#import <Masonry/Masonry.h>
#import <XFDCommon/UIColor+Extension.h>
#import <XFDCommon/UIColor+Extension.h>

@interface XFDNodeMenu()

@property(nonatomic,strong)NSMutableArray *btns;
@property(nonatomic,strong)NSArray *titles;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NodesBlock nodeBlock;
@property(nonatomic,copy)imageBlock imgB;
@property(nonatomic,copy)labelBlock titleB;
@property(nonatomic,copy)labelBlock contentB;
@property(nonatomic,copy)tapClick tapClickB;

@end

@implementation XFDNodeMenu

+ (instancetype)nodeMenuTitle:(NSString *)title nodes:(NodesBlock)block andTitles:(NSArray *)discriptions frame:(CGRect)frame{
    return [[self alloc] initWithFrame:frame title:title nodes:block andTitles:discriptions];
}
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title nodes:(NodesBlock)block andTitles:(NSArray *)discriptions{
    self = [super initWithFrame:frame];
    if (self) {
        self.nodeBlock = block;
        self.btns = [NSMutableArray arrayWithCapacity:discriptions.count];
        self.titles = discriptions;
        self.title = title;
        [self UI];
    }
    return self;
}

+ (instancetype)nodeMenuLeftImageView:(imageBlock)imgBlock title:(labelBlock)titleBlock content:(labelBlock)contentBlock tapClick:(tapClick)tapBlock small:(BOOL)small{
    return [[self alloc] initLeftImageView:imgBlock title:titleBlock content:contentBlock tapClick:tapBlock small:small];
}
- (instancetype)initLeftImageView:(imageBlock)imgBlock title:(labelBlock)titleBlock content:(labelBlock)contentBlock tapClick:(tapClick)tapBlock small:(BOOL)small{
    self = [super init];
    if (self) {
        /// 点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [self addGestureRecognizer:tap];
        self.backgroundColor = [UIColor whiteColor];
        self.imgB = imgBlock;
        self.titleB = titleBlock;
        self.contentB = contentBlock;
        self.tapClickB = tapBlock;
        
        /// 图标
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        self.imgB ? self.imgB(imageView) : nil;
        
        /// 标题
        UILabel *title = [self createLabel];
        title.textColor = [UIColor HB_ColorWithHexString:@"#333333"];
        self.titleB ? self.titleB(title) : nil;
        
        /// 内容
        UILabel *content = [self createLabel];
        title.textColor = [UIColor HB_ColorWithHexString:@"#666666"];
        self.contentB ? self.contentB(content) : nil;
        
        if(small){
            title.font = [UIFont systemFontOfSize:15];
            content.font = [UIFont systemFontOfSize:24];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(17);
                make.width.height.mas_equalTo(31);
                make.top.equalTo(self).offset(8);
            }];
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(12);
                make.left.equalTo(imageView.mas_right).offset(5);
                make.right.equalTo(self).offset(-15);
//                make.height.mas_equalTo(15);
            }];
            [content mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(title);
                make.top.equalTo(title.mas_bottom).offset(13);
                make.height.mas_equalTo(18);
            }];
        }else{
            title.font = [UIFont systemFontOfSize:17];
            content.font = [UIFont systemFontOfSize:30];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(19);
                make.width.height.mas_equalTo(39);
                make.top.equalTo(self).offset(14);
            }];
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(17);
                make.left.equalTo(imageView.mas_right).offset(5);
                make.right.equalTo(self).offset(-19);
                make.height.mas_equalTo(16);
            }];
            [content mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(title);
                make.top.equalTo(title.mas_bottom).offset(26);
                make.height.mas_equalTo(23);
            }];
        }

    }
    return self;
}

- (void)UI{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.width.height.mas_equalTo(14);
        make.top.equalTo(self).offset(10);
    }];
    UILabel *titleLabel = [self createLabel];
    titleLabel.text = self.title;
    titleLabel.textColor = [UIColor HB_ColorWithHexString:@"#333333"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(5);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(imageView);
    }];
    
    // 总列数
    int totalColumns = 4;
    // 每一格的尺寸
    CGFloat cellW = 44;
    CGFloat cellH = 44;
    // 间隙
    CGFloat margin = (self.frame.size.width - totalColumns * cellW) / (totalColumns + 1);
    
    for (int i =0; i<self.titles.count; i++) {
        // 计算行号  和  列号
        int row = i / totalColumns;
        int col = i % totalColumns;
        //根据行号和列号来确定 子控件的坐标
        CGFloat cellX = margin + col * (cellW + margin);
        CGFloat cellY = row * (cellH + margin);
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius = 5;
        [btn setBackgroundColor:[UIColor HB_ColorWithHexString:@"#f6f6f6"]];
        btn.clipsToBounds = YES;
        btn.titleLabel.font = [UIFont systemFontOfSize:24];
        btn.frame = CGRectMake(cellX, cellY+40, cellW, cellH);
        [self.btns addObject:btn];
        [self addSubview:btn];
        
        UILabel *label = [self createLabel];
        label.textColor = [UIColor HB_ColorWithHexString:@"#555555"];
        label.font = [UIFont systemFontOfSize:14];
        label.text = self.titles[i];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btn);
            make.height.mas_equalTo(20);
            make.top.equalTo(btn.mas_bottom).offset(14);
        }];
    }
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor HB_ColorWithHexString:@"#efefef"];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(self);
    }];
    self.nodeBlock ? self.nodeBlock(imageView,self.btns) : nil;
}

- (void)tapClick{
    self.tapClickB ? self.tapClickB() : nil;
}

- (UILabel *)createLabel{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentRight;
    label.adjustsFontSizeToFitWidth = YES;
    [self addSubview:label];
    return label;
}
@end
